package com.example.demo.leetcode.classics150;

import java.util.LinkedList;
import java.util.List;

/**
 * ******************************************************
 *
 * @author liugh9
 * @version 1.0
 * @classname _101电话号码的字母组合
 * @description
 * @date 2023/08/10 14:19
 * <p>
 * ******************************************************
 */
public class _101电话号码的字母组合 {

    List<String> result = new LinkedList<>();
    StringBuilder strace = new StringBuilder();

    public List<String> letterCombinations(String digits) {
        if (digits == null || digits.length() == 0) {
            return result;
        }
        backtrack(digits, 0);
        return result;
    }

    private void backtrack(String digits, int start) {
        int length = digits.length();
        if (length == strace.length()) {
            result.add(strace.toString());
            return;
        }
        if (start >= length) {
            return;
        }
        char[] stringByChar = getStringByChar(digits.charAt(start));

        for (char c : stringByChar) {
            strace.append(c);
            backtrack(digits, start + 1);
            strace.deleteCharAt(strace.length()-1);
        }
    }


    private char[] getStringByChar(char c) {
        String res;
        switch (c) {
            case '2':
                res = "abc";
                break;
            case '3':
                res = "def";
                break;
            case '4':
                res = "ghi";
                break;
            case '5':
                res = "jkl";
                break;
            case '6':
                res = "mno";
                break;
            case '7':
                res = "pqrs";
                break;
            case '8':
                res = "tuv";
                break;
            case '9':
                res = "wxyz";
                break;
            default:
                res = "";
                break;
        }
        return res.toCharArray();
    }
}
